package patrol.mobile.com.mobilepatrol.activity.route_query;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;

import patrol.mobile.com.mobilepatrol.DBHelper;
import patrol.mobile.com.mobilepatrol.R;

/**
 * Created by apple on 18/1/24.
 */

public class TypeAdapter extends BaseExpandableListAdapter {
    private final DBHelper db;
    private Context context;
    private List<RouteType> mList;
    public TypeAdapter(Context context,List<RouteType> mList) {
        this.context=context;
        this.mList = mList;
        db =new  DBHelper(context);
    }

    @Override
    public int getGroupCount() {
        return mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).routeManages.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mList.get(groupPosition).routeManages.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.parent,null);
        }
        TextView tv= (TextView) convertView.findViewById(R.id.tv);
        TextView tv_mail= (TextView) convertView.findViewById(R.id.tv_mail);
        RouteType struct= mList.get(groupPosition);
        double childMiles = 0;
        RouteType rt = mList.get(groupPosition);
        for (int i=0;i< rt.routeManages.size();i++){
            RouteManage rm = rt.routeManages.get(i);
            childMiles = childMiles+rm.getCustodyRouteMileage();
        }
        RouteManage rm0 = mList.get(groupPosition).routeManages.get(0);
        tv.setText(struct.custodyRouteType);
//        Double d = db.getRouteManageMiles(struct.custodyRouteType,rm0.getOrgName());
        tv_mail.setText("共"+childMiles+"km");

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_route,null);
        }
        TextView tv1= (TextView) convertView.findViewById(R.id.tv1);
        TextView tv2= (TextView) convertView.findViewById(R.id.tv2);
        TextView index = convertView.findViewById(R.id.index);
        RouteManage rm = mList.get(groupPosition).routeManages.get(childPosition);
        tv1.setText(rm.routeCode+" ( "+rm.startPot+" - "+rm.endPot+" )");
        tv2.setText("共"+rm.custodyRouteMileage+"km");
        index.setText(rm.index+"");
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
